home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / util / libs / shutdown.lzh / shutdown4.1 / src / shutdown_lib / shutdown_library.c < prev    next >
C/C++ Source or Header  |  1996-09-22  |  5KB  |  201 lines

  1. /*
  2.    shutdown_library.c --- simple shutdown library
  3.  
  4.    This library is compatible with the shutdown library
  5.    written by Olaf Barthel (AmiNet:util/boot/Shutdown2_0.lha).
  6.    It is _NOT_ an update but a twin which is reduced to
  7.    calling the hook functions. You can use the library of your
  8.    preference. An update to the original library will be
  9.    released later (see IDEAS, more ideas are welcome).
  10.  
  11.    This module is based on code from the original library
  12.    Copyright © 1992 by Olaf `Olsen' Barthel (not for commercial use)
  13.  
  14.    Written by Bernhard Fastenrath (fasten@shw.com)
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <exec/semaphores.h>
  20. #include <utility/hooks.h>
  21. #include <proto/exec.h>
  22. #include <string.h>
  23.  
  24. #if defined (__GNUC__)
  25. #include <stabs.h>
  26. #endif
  27.  
  28. #include "shutdown_library.h"
  29.  
  30. struct ExecBase *SysBase;
  31. struct DosLibrary *DOSBase = NULL;
  32. struct Library *UtilityBase = NULL;
  33. struct Library *ShutdownBase;
  34.  
  35. MinList ShutdownList;
  36. SignalSemaphore AccessLock;
  37. SignalSemaphore    ShutdownLock;
  38. BYTE Closing = 0;
  39.  
  40. #if defined (__GNUC__)
  41. const BYTE LibName[] = "shutdown.library";
  42. const BYTE LibIdString[] = "$VER: shutdown.library 2.3 (10-26-95)";
  43. const UWORD LibVersion = 2;
  44. const UWORD LibRevision = 3;
  45. #endif
  46.  
  47. #if defined (__GNUC__)
  48. #define LIBRT
  49. #define REG(regname)
  50. #define STRUCT_MYLIB struct Library
  51. #elif defined (__SASC_60)
  52. #define LIBRT __saveds __asm
  53. #define REG(regname) register __ ## regname
  54. #define ADDTABL_1(name,arg1);
  55. #define ADDTABL_2(name,arg1,arg2);
  56. #define ADDTABL_3(name,arg1,arg2,arg3);
  57. #define ADDTABL_END();
  58. #define STRUCT_MYLIB struct MyLibrary
  59. #endif
  60.  
  61. /*** internal functions ***/
  62.  
  63. static void
  64. CallClientHooks (void)
  65. {
  66.   if (ShutdownList.mlh_Head -> mln_Succ)
  67.   {
  68.     ShutdownInfo *Info, *NextInfo;
  69.     ULONG Mode;
  70.  
  71.     Info = (ShutdownInfo *) ShutdownList.mlh_Head;
  72.     while (NextInfo = (ShutdownInfo *) Info -> sdi_Node.mln_Succ)
  73.     {
  74.       Mode = SD_EXIT;
  75.       CallHook (Info -> sdi_Hook, &Mode, NULL);
  76.       Info = NextInfo;
  77.     }
  78.   }
  79. }
  80.  
  81. static void
  82. cleanup (void)
  83. {
  84.   if (DOSBase)
  85.     CloseLibrary ((struct Library *) DOSBase);
  86.   DOSBase = NULL;
  87.   if (UtilityBase)
  88.     CloseLibrary ((struct Library *) UtilityBase);
  89.   UtilityBase = NULL;
  90. }
  91.  
  92. /*** library interface ***/
  93.  
  94. int LIBRT
  95. __UserLibInit (REG(a6) STRUCT_MYLIB *libbase)
  96. {
  97.   SysBase = *(struct ExecBase **)4;
  98.   ShutdownBase = (struct Library *) libbase;
  99.  
  100.   if (!(DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 37)))
  101.     return 1;
  102.   if (!(UtilityBase = OpenLibrary ("utility.library", 37)))
  103.   {
  104.     cleanup ();
  105.     return 1;
  106.   }
  107.   InitSemaphore(&AccessLock);
  108.   InitSemaphore(&ShutdownLock);
  109.   NewList ((struct List *) &ShutdownList);
  110.   return 0; /* success */
  111. }
  112.  
  113. void LIBRT
  114. __UserLibCleanup (REG(a6) STRUCT_MYLIB *libbase)
  115. {
  116.   cleanup ();
  117. }
  118.  
  119. ADDTABL_1(LIBRexxDispatch,a0);
  120.  
  121. LONG LIBRT
  122. LIBRexxDispatch (REG(a0) void *Message)
  123. {
  124.   return 0;
  125. }
  126.  
  127. ADDTABL_3(LIBAddShutdownInfoTagList,a0,a1,a2);
  128.  
  129. APTR LIBRT
  130. LIBAddShutdownInfoTagList (REG(a0) Hook *Hook, REG(a1) STRPTR Name, REG(a2) TagItem *TagList)
  131. {
  132.   if (!Closing)
  133.   {
  134.     if (Hook && Name && TagList)
  135.     {
  136.        ShutdownInfo *Info;
  137.        LONG Len = strlen (Name);
  138.  
  139.        if (Info = (ShutdownInfo *) AllocVec (sizeof (ShutdownInfo) + Len + 1, MEMF_PUBLIC | MEMF_CLEAR))
  140.        {
  141.          ObtainSemaphore (&AccessLock);
  142.          Info -> sdi_Hook = Hook;
  143.          Info -> sdi_Name = (STRPTR)(Info + 1);
  144.          strcpy (Info -> sdi_Name, Name);
  145.          AddTail ((struct List *) &ShutdownList, (struct Node *) Info);
  146.          ReleaseSemaphore (&AccessLock);
  147.          return Info;
  148.        }
  149.     }
  150.   }
  151.   return NULL;
  152. }
  153.  
  154. ADDTABL_1(LIBRemShutdownInfo,a0);
  155.  
  156. LONG LIBRT
  157. LIBRemShutdownInfo (REG(a0) ShutdownInfo *Info)
  158. {
  159.   if (!Closing && Info)
  160.   {
  161.      ShutdownInfo *node, *next;
  162.  
  163.      ObtainSemaphore (&AccessLock);
  164.      node = (ShutdownInfo *) ShutdownList.mlh_Head;
  165.      while (next = (ShutdownInfo *) node -> sdi_Node.mln_Succ)
  166.      {
  167.        if (Info == (ShutdownInfo *) node)
  168.        {
  169.          Remove ((struct Node *) Info);
  170.          FreeVec (Info);
  171.          ReleaseSemaphore (&AccessLock);
  172.          return TRUE;
  173.        }
  174.        node = next;
  175.      }
  176.      ReleaseSemaphore (&AccessLock);
  177.   }
  178.   return FALSE;
  179. }
  180.  
  181. ADDTABL_1(LIBShutdown,d0);
  182.  
  183. LONG LIBRT
  184. LIBShutdown (REG(d0) ULONG mode)
  185. {
  186.   if (!AttemptSemaphore (&ShutdownLock))
  187.     return FALSE;
  188.   else
  189.   {
  190.     /* mode is ignored by this library but
  191.        not by the original shutdown.libray */
  192.  
  193.     CallClientHooks ();
  194.     ReleaseSemaphore (&ShutdownLock);
  195.     return TRUE;
  196.   }
  197.   return FALSE;
  198. }
  199.  
  200. ADDTABL_END();
  201.